package liang.jiaxin.com.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtils extends org.springframework.util.NumberUtils{

	public static <T extends Number> T defaultIfNull(T value, T defaultValue) {
		return isNumberNotNull(value) ? value : defaultValue;
	}
	
	public static boolean isNumberNotNull(Number value) {
		return value != null ? true : false;
	}
	
	public static boolean isIntNotNullAndGTZero(Integer value) {
		return isNumberNotNull(value) && value > 0 ? true : false;
	}
	
	public static boolean isLongNotNullAndGTZero (Long value) {
		return isNumberNotNull(value) && value > 0 ? true : false;
	}

	/**
	 * 隐藏并替换所有的身份证号码
	 * @param contentStr
	 * @return
	 */
	public static String hideAllIdCardNum(String contentStr){
		Pattern pattern = Pattern.compile("(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)");
		Matcher matcher = pattern.matcher(contentStr);
		StringBuffer sb = new StringBuffer();
		try {
			while(matcher.find()) {
				String idCardStr = matcher.group();
				int len=idCardStr.length();
				if(len>=9){
					idCardStr =  idCardStr.replaceAll("(.{"+(len<12?3:6)+"})(.*)(.{4})", "$1" + "****" + "$3");
				}
				matcher.appendReplacement(sb,idCardStr);
			}
			matcher.appendTail(sb);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return sb.toString();
	}
	
}